package com.cetool.autocms.service.tradesequence.impl;

import cn.hutool.core.bean.BeanUtil;
import com.cetool.autocms.common.PageRequest;
import com.cetool.autocms.enumeration.OperateEnum;
import com.cetool.autocms.mapper.tradesequence.NoticeDataMapper;
import com.cetool.autocms.mapper.tradesequence.TradeSequenceMapper;
import com.cetool.autocms.pojo.dto.tradesequence.TradeSequenceInfoDTO;
import com.cetool.autocms.pojo.param.tradesequence.DeleteTradeSequenceParam;
import com.cetool.autocms.pojo.param.tradesequence.QueryTradeSequenceListParam;
import com.cetool.autocms.pojo.param.tradesequence.SaveTradeSequenceParam;
import com.cetool.autocms.pojo.param.tradesequence.UpdateTradeSequenceParam;
import com.cetool.autocms.pojo.request.tradesequence.DeleteTradeSequenceRequest;
import com.cetool.autocms.pojo.request.tradesequence.QueryTradeSequenceListRequest;
import com.cetool.autocms.pojo.request.tradesequence.SaveTradeSequenceRequest;
import com.cetool.autocms.pojo.request.tradesequence.UpdateTradeSequenceRequest;
import com.cetool.autocms.service.tradesequence.TransactionSerialService;
import com.cetool.autocms.util.BeansUtil;
import com.github.pagehelper.PageSerializable;
import com.github.pagehelper.page.PageMethod;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 交易序列管理服务实现层
 *
 * @author 戴越
 * @date 2021/11/10
 */
@Service
@Slf4j
public class TransactionSerialServiceImpl implements TransactionSerialService {
    /**
     * 注入交易序列管理持久层
     */
    @Autowired
    private TradeSequenceMapper tradeSequenceMapper;
    /**
     * 注入公告数据管理持久层
     */
    @Autowired
    private NoticeDataMapper noticeDataMapper;

    @Override
    public PageSerializable<TradeSequenceInfoDTO> listPageTradeSequence(PageRequest<QueryTradeSequenceListRequest> request) {
        QueryTradeSequenceListParam param = new QueryTradeSequenceListParam();
        BeanUtil.copyProperties(request.getData(), param);
        PageMethod.startPage(request.getPageNum(), request.getPageSize());
        return BeansUtil.pageCopy(new PageSerializable<>(tradeSequenceMapper.getTradeSequenceList(param)), TradeSequenceInfoDTO.class);
    }

    @Override
    public OperateEnum saveTradeSequence(SaveTradeSequenceRequest request) {
        SaveTradeSequenceParam param = new SaveTradeSequenceParam();
        BeanUtil.copyProperties(request, param);
        tradeSequenceMapper.saveTradeSequence(param);
        return OperateEnum.ADD_SUCCESS;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public OperateEnum deleteTradeSequence(DeleteTradeSequenceRequest request) {
        DeleteTradeSequenceParam param = new DeleteTradeSequenceParam();
        BeanUtil.copyProperties(request, param);
        //1、删除交易序列
        tradeSequenceMapper.deleteTradeSequence(param);
        //2、删除公告数据
        noticeDataMapper.deleteNoticeData(param);
        return OperateEnum.DELETE_SUCCESS;
    }

    @Override
    public OperateEnum updateTradeSequence(UpdateTradeSequenceRequest request) {
        UpdateTradeSequenceParam param = new UpdateTradeSequenceParam();
        BeanUtil.copyProperties(request, param);
        tradeSequenceMapper.updateTradeSequence(param);
        return OperateEnum.UPDATE_SUCCESS;
    }
}
